package com.service;

import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.apache.tomcat.util.buf.UDecoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.UsersDaoImpl;
import com.entity.User;
@Transactional
@Service
public class UserServiceImpl {
	@Resource
	private UsersDaoImpl usersDaoImpl;
	
	/*
	 * 注册，对从jsp页面传递过来的数据进行验证：输入的信息都符合正确，就返回true
	 */
	public String userRegister(User user){
//		if(user.getUsername()==null || user.getUsername().equals("")){
//			return "姓名不能为空";
//		}
//		if(user.getPassword()==null || user.getPassword().equals("")){
//			return "密码不能为空";
//		}
//		if(user.getSex()==null || user.getSex().equals("")){
//			return "性别不能为空";
//		}
//		if(user.getAddress()==null || user.getAddress().equals("")){
//			return "地址不能为空";
//		}
//		if(user.getPhone()==null || user.getPhone().equals("")){
//			return "电话不能为空";
//		}
//		if(user.getHobby()==null || user.getHobby().equals("")){
//			return "爱好不能为空";
//		}
//		Pattern pattern = Pattern.compile("^20\\d{8}$");
//		if (!(pattern.matcher(user.getPhone()).matches())) {
//			return "电话号码必须由10位数字组成";
//		}
//		pattern = Pattern.compile("(^[\\u4e00-\\u9fa5]{1}[\\u4e00-\\u9fa5\\.·。]{0,8}[\\u4e00-\\u9fa5]{1}$)|(^[a-zA-Z]{1}[a-zA-Z\\s]{0,8}[a-zA-Z]{1}$)");
//		if (!(pattern.matcher(user.getUsername()).matches())) {
//			return "请填写您的真实姓名，长度在20个字符以内";
//		}
//		pattern = Pattern.compile("^[a-zA-Z]\\w{5,17}$");
//		if (!(pattern.matcher(user.getPassword()).matches())) {
//			return "密码以字母开头，长度在6~18之间，只能包含字母、数字和下划线";
//		}
//		if (true == findUserByPhone(user.getPhone())) {
//			return "该号码已经注册过了";
//		}
		usersDaoImpl.save(user);
		System.out.println("service"+user);
		return "0";
		
	}
	/*
	 * 根据电话号码查询用户是否存在
	 */
	private boolean findUserByPhone(String phone) {
		try{
			User user = usersDaoImpl.findByPhone(phone);
			if(user !=null){
				return true;//可以找到该用户
			}else{
				return false;//不存在该用户
			}
		}catch (Exception e){
			return false;
		}
	}
	
	/*
	 * 修改信息:先获取该用户信息
	 */
	public User findByPhone(String phone){
		User user = usersDaoImpl.findByPhone(phone);
		return user;
		
	}
	public String userUpdate(User user){
		usersDaoImpl.update(user);
		System.out.println("service"+user);
		return "user_update";
	}
	
	
	/*
	 * 登录
	 */
	public boolean Login(String phone,String password){
		if(this.usersDaoImpl.Login(phone, password)){
			return true;
		}else{
			return false;
		}
	}
	
	public User getUserByPhone(String phone){
		return this.usersDaoImpl.getUserByPhone(phone);
	}
	public boolean getByPhone(String phone){
		if(this.usersDaoImpl.getByPhone(phone)){
			return true;
		}else{
			return false;
		}
	}
	
	
	/*
	 * 注销账号
	 */
	public boolean userDelete(User user){
		if(this.usersDaoImpl.userDel(user)){
			return true;
		}else{
			return false;
		}
		
	}
	
	
	
}
